# -*- coding:utf-8 -*-
import sys
from matplotlib import pyplot as plt
import cv2 as cv
import numpy as np


if __name__ == '__main__':


    image = cv.imread(r"E:\studylife\detectflaws\code\imgEnhance\img4.jpg", cv.IMREAD_GRAYSCALE)

    # 绘制原图直方图
    plt.hist(image.ravel(), 256, [0, 256])
    plt.title('Origin Image')
    #plt.show()
    # 进行均衡化并绘制直方图
    image_result3 = cv.equalizeHist(image)  # 普通直方图均衡化
    # 自适应直方图均衡化
    clahe = cv.createCLAHE(clipLimit=2, tileGridSize=(2, 2))  # clipLimit：这是对比度限制的阈值
    image_result = clahe.apply(image)  # tileGridSize：将输入图像划分为M × N块，然后对每个局部块应用直方图均衡化

    clahe2 = cv.createCLAHE(clipLimit=20, tileGridSize=(2, 2))  # clipLimit：这是对比度限制的阈值
    image_result2 = clahe2.apply(image)  # tileGridSize：将输入图像划分为M × N块，然后对每个局部块应用直方图均衡化

    image = cv.resize(image, (640, 480))
    image_result = cv.resize(image_result, (640, 480))
    image_result2 = cv.resize(image_result2, (640, 480))
    image_result3 = cv.resize(image_result3, (640, 480))

    plt.hist(image_result.ravel(), 256, [0, 256])
    plt.title('Equalized Image')
    #plt.show()
    # 展示均衡化前后的图片
    cv.imshow('Origin Image', image)
    image_result = cv.applyColorMap(image_result, cv.COLORMAP_JET)
    cv.imshow('Equalized Image', image_result)
    cv.imshow('Equalized Image2', image_result2)
    cv.imshow('Equalized Image3', image_result3)

    cv.waitKey(0)
    cv.destroyAllWindows()